
using Core.Configurations;
using Core.Enums;
using Core.Interfaces;
using Hangfire;
using Infrastructure.Data;
using Infrastructure.Repositories;
using Infrastructure.Services;
using Microsoft.EntityFrameworkCore;

namespace Web
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();
            builder.Services.AddDbContext<ApplicationDbContext>(
                opt=>opt.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))
                );
            builder.Services.Configure<CacheConfiguration>(builder.Configuration.GetSection(nameof(CacheConfiguration)));

            //for in-memory caching
            builder.Services.AddMemoryCache();
            builder.Services.AddTransient<MemoryCacheService>();
            builder.Services.AddTransient<RedisCacheService>();
            builder.Services.AddTransient<Func<CacheTech, ICacheService>>(sp => key =>
            {
                switch (key)
                {
                    case CacheTech.Redis:
                        return sp.GetService<RedisCacheService>();
                    case CacheTech.Memory:
                        return sp.GetService<MemoryCacheService>();
                    default:
                        return sp.GetService<MemoryCacheService>();
                }
            }
            );

            //hangfire
            builder.Services.AddHangfire(x => x.UseSqlServerStorage(builder.Configuration.GetConnectionString("DefaultConnection")));
            builder.Services.AddHangfireServer();

            builder.Services.AddTransient(typeof(IGenericRepository<>), typeof(GenericRepository<>));
            builder.Services.AddTransient<ICustomerRepository, CustomerRepository>();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseAuthorization();

            app.UseHangfireDashboard("/jobs");
            app.MapControllers();

            app.Run();
        }
    }
}
